var TEXT_MAX_SEARCH_TIME = 10000; //搜索控件时超时时间 http://doc.autoxjs.com/#/widgetsBasedAutomation?id=uiselectorfindonetimeout
var clickUtils = require("./clickUtils");
var textUtils = {
  existsText(content, ignoreLog) {
    if (text(content).exists()) {
      var TextUI = text(content).findOne(TEXT_MAX_SEARCH_TIME);
      if (!ignoreLog && TextUI) {
        console.log("existsText", "找到：", content, "/", TextUI.text());
      }
      return TextUI;
    } else {
      return false;
    }
  },
  existsTextStartsWith(content, ignoreLog) {
    if (textStartsWith(content).exists()) {
      var TextUI = textStartsWith(content).findOne(TEXT_MAX_SEARCH_TIME);
      if (!ignoreLog && TextUI) {
        console.log(
          "existsTextStartsWith",
          "找到：",
          content,
          "/",
          TextUI.text()
        );
      }
      return TextUI;
    } else {
      return false;
    }
  },
  existsTextEndsWith(content, ignoreLog) {
    if (textEndsWith(content).exists()) {
      var TextUI = textEndsWith(content).findOne(TEXT_MAX_SEARCH_TIME);
      if (!ignoreLog && TextUI) {
        console.log(
          "existsTextEndsWith",
          "找到：",
          content,
          "/",
          TextUI.text()
        );
      }
      return TextUI;
    } else {
      return false;
    }
  },
  existsTextMatches(contentRegExp, ignoreLog) {
    if (textMatches(contentRegExp).exists()) {
      var TextUI = textMatches(contentRegExp).findOne(TEXT_MAX_SEARCH_TIME);
      if (!ignoreLog) {
        console.log(
          "existsTextMatches",
          "找到：",
          contentRegExp,
          "/",
          TextUI.text()
        );
      }
      return TextUI;
    } else {
      return false;
    }
  },
};
var clickMethods = [
  ["existsText", "clickText"],
  ["existsTextStartsWith", "clickTextStartsWith"],
  ["existsTextEndsWith", "clickTextEndsWith"],
  ["existsTextMatches", "clickTextMatches"],
];
var oneMethods = [
  ["existsText", "existsOneText"],
  ["existsTextStartsWith", "existsOneTextStartsWith"],
  ["existsTextEndsWith", "existsOneTextEndsWith"],
  ["existsTextMatches", "existsOneTextMatches"],
];
var allMethods = [
  ["existsText", "existsAllText"],
  ["existsTextStartsWith", "existsAllTextStartsWith"],
  ["existsTextEndsWith", "existsAllTextEndsWith"],
  ["existsTextMatches", "existsAllTextMatches"],
];
clickMethods.forEach(function (method) {
  var oldMethod = method[0];
  var newMethod = method[1];
  textUtils[newMethod] = function (content, ignoreLog) {
    var TextUI = this[oldMethod](content, true);
    if (TextUI) {
      if (!ignoreLog) {
        console.log(newMethod, "找到:", content, "/", TextUI.text());
      }
      clickUtils.clickTextUI(TextUI);
      return TextUI;
    } else {
      return false;
    }
  };
  return false;
});
oneMethods.forEach(function (method) {
  var oldMethod = method[0];
  var newMethod = method[1];
  textUtils[newMethod] = function (contents, ignoreLog) {
    if (!Array.isArray(contents)) {
      contents = [contents];
    }
    for (var index = 0; index < contents.length; index++) {
      var content = contents[index];
      var TextUI = this[oldMethod](content, true);
      if (TextUI) {
        if (!ignoreLog) {
          console.log(
            newMethod,
            "找到",
            contents.join("|"),
            "/",
            TextUI.text()
          );
        }
        return TextUI;
      }
    }
    return false;
  };
});
allMethods.forEach(function (method) {
  var oldMethod = method[0];
  var newMethod = method[1];
  var findText = [];
  textUtils[newMethod] = function (contents, ignoreLog) {
    if (!Array.isArray(contents)) {
      contents = [contents];
    }
    for (var index = 0; index < contents.length; index++) {
      var content = contents[index];
      var TextUI = this[oldMethod](content, true);
      if (!TextUI) {
        return false;
      } else {
        findText.push(TextUI.text());
      }
    }
    if (!ignoreLog) {
      console.log(
        newMethod,
        "全部找到",
        contents.join("|"),
        "/",
        findText.join("|")
      );
    }
    return true;
  };
});

module.exports = textUtils;
